package login

import (
	"gitee.com/tomatomeatman/golang-repository/bricks3/function/configutil"
	"gitee.com/tomatomeatman/golang-repository/bricks3/utils/ginutil"
	"github.com/gin-gonic/gin"
)

/**
 * 初始化
 */
func init() {
	controller := &LoginController{}
	ginutil.AddUrl("/login/in", controller.In, ginutil.POST)
	ginutil.AddUrl("/login/out", controller.Out, ginutil.POST)
	ginutil.AddUrl("/login/check", controller.Check, ginutil.POST)
	ginutil.AddUrl("/login/heartbeat", controller.Heartbeat, ginutil.POST)
	ginutil.AddUrl("/login/info", controller.GetLogin, ginutil.POST)
	ginutil.AddUrl("/login/info/current", controller.GetCurrentLogin, ginutil.POST)
	ginutil.AddUrl("/login/getid", controller.GetUserId, ginutil.POST)
}

// @Controller 登录服务控制器
type LoginController struct {
}

// #region @Api {title=用户登录 explain=如果用户和密码正确则返回同行令牌}
// @param {name=nameOrNo dataType=string paramType=query explain=名称或工号 required=true}
// @param {name=pass dataType=string paramType=query explain=密码 required=true}
// @param {name=owner dataType=string paramType=query explain=用户来源表 required=true}
// @param {name=device dataType=int paramType=query explain=设备类型,1:PC,2:手机,3:平板,4.....}
// @param {name=resultInfo dataType=int paramType=query explain=是否返回用户信息}
// @return {type=(bool, string, interface{}) explain=返回令牌}
// @RequestMapping {name=In type=(POST, GET) value=/login/in}
// #endregion
func (lc LoginController) In(ctx *gin.Context) (bool, string, interface{}) {
	sNameOrNo := ginutil.GetParam(ctx, "sNameOrNo", "").(string)
	sPass := ginutil.GetParam(ctx, "sPass", "").(string)
	iDevice := ginutil.GetParam(ctx, "iDevice", 1).(int)
	iResultInfo := ginutil.GetParam(ctx, "iResultInfo", 2).(int)

	return LoginServer{}.In(ctx, sNameOrNo, sPass, iDevice, iResultInfo)
}

// #region @Api {title=用户登出 explain=使提交的令牌失效}
// @param {name=token dataType=string paramType=query explain=令牌 required=true}
// @return {type=(bool, string, interface{})}
// @RequestMapping {name=Out type=(POST,GET) value=/login/check}
// #endregion
func (lc LoginController) Out(ctx *gin.Context) (bool, string, interface{}) {
	token := ginutil.GetParam(ctx, "token", "").(string)
	return LoginServer{}.Out(token)
}

// #region @Api {title=验证令牌 explain=判断token有效并返回用户信息}
// @param {name=token dataType=string paramType=query explain=令牌 required=true}
// @param {name=key dataType=string paramType=query explain=内部密钥 required=false}
// @return {type=(bool, string, interface{})}
// @RequestMapping {name=Check type=POST value=/login/check}
// #endregion
func (lc LoginController) Check(ctx *gin.Context) (bool, string, interface{}) {
	token := ginutil.GetParam(ctx, "token", "").(string)

	bl, msg, res := LoginServer{}.Check(token)
	if !bl {
		return bl, msg, res
	}

	appKey := configutil.ReadConfigKey("", "App", "InsideKey", "12345").(string)
	key := ginutil.GetParam(ctx, "key", "").(string)
	if (key == "") || (key != appKey) {
		return bl, msg, res
	}

	return LoginServer{}.GetLogin(token)
}

// #region @Api {title=取登录用户信息 explain=注意:限制为内部系统访问}
// @param {name=key}
// @param {name=token dataType=string paramType=query explain=令牌 required=true}
// @return {type=(bool, string, interface{})}
// @RequestMapping {name=GetLogin type=POST value=/login/info}
// #endregion
func (lc LoginController) GetLogin(ctx *gin.Context) (bool, string, interface{}) {
	appKey := configutil.ReadConfigKey("", "App", "InsideKey", "12345").(string)

	key := ginutil.GetParam(ctx, "key", "").(string)
	if (key == "") || (key != appKey) {
		return false, "验证密钥错误", 9000
	}

	token := ginutil.GetParam(ctx, "token", "").(string)
	return LoginServer{}.GetLogin(token)
}

// #region @Api {title=登录心跳操作 explain=token存在则更新并返回true,没有则返回false}
// @param {name=token dataType=string paramType=query explain=令牌 required=true}
// @return {type=(bool, string, interface{})}
// @RequestMapping {name=Heartbeat type=(POST,GET) value=/login/heartbeat}
// #endregion
func (lc LoginController) Heartbeat(ctx *gin.Context) (bool, string, interface{}) {
	token := ginutil.GetParam(ctx, "token", "").(string)
	return LoginServer{}.Heartbeat(token)
}

// #region @Api {title=取当前登录用户简洁信息}
// @return {type=(bool, string, interface{})}
// @RequestMapping {name=GetCurrentLogin type=POST value=/login/info/current}
// #endregion
func (lc LoginController) GetCurrentLogin(ctx *gin.Context) (bool, string, interface{}) {
	token := ginutil.GetParam(ctx, "token", "").(string) //获取request对象中的参数,优先: 头信息->参数-->属性
	if token == "" {
		return false, "令牌已无效,限制获取登录信息", 8001
	}

	return LoginServer{}.GetLogin(token)
}

// #region @Api {title=根据用户和密码取对应的用户编号}
// @param {name=sNameOrNo dataType=string paramType=query explain=名称或工号 required=true}
// @param {name=pass dataType=string paramType=query explain=密码 required=true}
// @param {name=owner dataType=string paramType=query explain=用户来源表 required=true}
// @return {type=(bool, string, interface{})}
// @RequestMapping {name=GetUserId type=POST value=/login/getid}
// #endregion
func (lc LoginController) GetUserId(ctx *gin.Context) (bool, string, interface{}) {
	nameOrNo := ginutil.GetParam(ctx, "sNameOrNo", "").(string)
	pass := ginutil.GetParam(ctx, "sPass", "").(string)
	return LoginServer{}.GetUserId(nameOrNo, pass)
}
